package org.example.controller;

import org.apache.ibatis.annotations.Delete;
import org.example.pojo.PageBean;
import org.example.pojo.Result;
import org.example.pojo.Vendor;
import org.example.service.VendorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
@RequestMapping("/vendors")
public class VendorController {
    private static final Logger log = LoggerFactory.getLogger(VendorController.class);
    @Autowired
    private VendorService vendorService;

    /**
     * 查询供应商列表
     * @return
     */
    @GetMapping("/list")
    public Result list() {
        List<Vendor> list = vendorService.list();
        return Result.success(list);
    }

    /**
     * 分页查询供应商列表
     */
    @GetMapping("/list/page")
    public Result listPage(int pageNum, int pageSize, String vendorName) {
        PageBean<Vendor> list = vendorService.listPage(pageNum, pageSize, vendorName);
        return Result.success(list);
    }

    /**
     * 添加供应商
     */
    @PostMapping("/add")
    public Result add(@RequestBody Vendor vendor) {
        vendorService.add(vendor);
        return Result.success();
    }

    /**
     * 修改供应商
     */
    @PutMapping("/update")
    public Result update(@RequestBody Vendor vendor) {
        log.info("修改供应商，供应商信息为：{}", vendor);
        vendorService.update(vendor);
        return Result.success();
    }

    /**
     * 删除供应商
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        vendorService.delete(id);
        return Result.success();
    }

    /**
     * 批量删除商品
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteBatch")
    public Result batchDelete(@RequestBody List<Integer> ids){
        log.info("批量删除商品:{}",ids);
        vendorService.batchDelete(ids);
        return Result.success();
    }
}
